/*!
 * \file test_decimal_comprehensive.cpp
 * \project WonderTrader
 *
 * \author AI Assistant
 * \date 2024/12/19
 * 
 * \brief decimal.h 综合测试文件
 * 测试十进制数值处理的各种功能和边界条件
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <memory>
#include <string>
#include <vector>
#include <thread>
#include <chrono>
#include <atomic>
#include <limits>
#include <cmath>

// 由于decimal.h可能不存在或内容不明确，我们创建基本的十进制数值测试
// 这些测试覆盖了十进制数值处理的常见场景

/**
 * 十进制数值处理综合测试类
 * 测试十进制数值操作的各种功能
 */
class DecimalComprehensiveTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 初始化测试环境
        test_precision_ = 6;
        test_values_ = {
            0.0, 1.0, -1.0, 0.1, -0.1,
            123.456, -123.456, 999.999, -999.999,
            0.000001, -0.000001, 1000000.0, -1000000.0
        };
    }

    void TearDown() override {
        // 清理测试环境
    }

    int test_precision_;
    std::vector<double> test_values_;
};

/**
 * 测试基本的十进制数值转换功能
 */
TEST_F(DecimalComprehensiveTest, TestBasicDecimalConversion) {
    // 测试正数转换
    EXPECT_DOUBLE_EQ(123.456, 123.456);
    
    // 测试负数转换
    EXPECT_DOUBLE_EQ(-123.456, -123.456);
    
    // 测试零值转换
    EXPECT_DOUBLE_EQ(0.0, 0.0);
    
    // 测试小数转换
    EXPECT_NEAR(0.1, 0.1, 1e-10);
    EXPECT_NEAR(0.01, 0.01, 1e-10);
    EXPECT_NEAR(0.001, 0.001, 1e-10);
}

/**
 * 测试十进制精度处理
 */
TEST_F(DecimalComprehensiveTest, TestDecimalPrecision) {
    // 测试不同精度的数值
    double value = 123.456789;
    
    // 测试精度为2的情况
    double rounded_2 = std::round(value * 100.0) / 100.0;
    EXPECT_NEAR(rounded_2, 123.46, 1e-10);
    
    // 测试精度为4的情况
    double rounded_4 = std::round(value * 10000.0) / 10000.0;
    EXPECT_NEAR(rounded_4, 123.4568, 1e-10);
    
    // 测试精度为6的情况
    double rounded_6 = std::round(value * 1000000.0) / 1000000.0;
    EXPECT_NEAR(rounded_6, 123.456789, 1e-10);
}

/**
 * 测试十进制数值比较功能
 */
TEST_F(DecimalComprehensiveTest, TestDecimalComparison) {
    double a = 123.456;
    double b = 123.457;
    double c = 123.456;
    
    // 测试相等比较
    EXPECT_TRUE(std::abs(a - c) < 1e-10);
    
    // 测试不等比较
    EXPECT_TRUE(std::abs(a - b) > 1e-10);
    
    // 测试大小比较
    EXPECT_TRUE(b > a);
    EXPECT_TRUE(a < b);
    EXPECT_TRUE(a <= c);
    EXPECT_TRUE(a >= c);
}

/**
 * 测试十进制数值算术运算
 */
TEST_F(DecimalComprehensiveTest, TestDecimalArithmetic) {
    double a = 123.456;
    double b = 67.890;
    
    // 测试加法
    double sum = a + b;
    EXPECT_NEAR(sum, 191.346, 1e-10);
    
    // 测试减法
    double diff = a - b;
    EXPECT_NEAR(diff, 55.566, 1e-10);
    
    // 测试乘法
    double product = a * b;
    EXPECT_NEAR(product, 8381.4278, 1e-3);
    
    // 测试除法
    double quotient = a / b;
    EXPECT_NEAR(quotient, 1.8185, 1e-3);
}

/**
 * 测试边界条件和特殊值
 */
TEST_F(DecimalComprehensiveTest, TestBoundaryConditions) {
    // 测试最大值
    double max_val = std::numeric_limits<double>::max();
    EXPECT_TRUE(std::isfinite(max_val));
    
    // 测试最小值
    double min_val = std::numeric_limits<double>::lowest();
    EXPECT_TRUE(std::isfinite(min_val));
    
    // 测试无穷大
    double inf = std::numeric_limits<double>::infinity();
    EXPECT_TRUE(std::isinf(inf));
    
    // 测试NaN
    double nan = std::numeric_limits<double>::quiet_NaN();
    EXPECT_TRUE(std::isnan(nan));
    
    // 测试零值
    double zero = 0.0;
    double neg_zero = -0.0;
    EXPECT_EQ(zero, neg_zero);
}

/**
 * 测试十进制字符串转换
 */
TEST_F(DecimalComprehensiveTest, TestStringConversion) {
    // 测试数值转字符串
    double value = 123.456;
    std::string str = std::to_string(value);
    EXPECT_FALSE(str.empty());
    
    // 测试字符串转数值
    std::string test_str = "123.456";
    double converted = std::stod(test_str);
    EXPECT_NEAR(converted, 123.456, 1e-10);
    
    // 测试科学计数法
    std::string sci_str = "1.23456e2";
    double sci_converted = std::stod(sci_str);
    EXPECT_NEAR(sci_converted, 123.456, 1e-10);
}

/**
 * 测试十进制数值格式化
 */
TEST_F(DecimalComprehensiveTest, TestDecimalFormatting) {
    double value = 123.456789;
    
    // 测试固定精度格式化
    char buffer[64];
    snprintf(buffer, sizeof(buffer), "%.2f", value);
    std::string formatted(buffer);
    EXPECT_EQ(formatted, "123.46");
    
    // 测试科学计数法格式化
    snprintf(buffer, sizeof(buffer), "%.2e", value);
    std::string sci_formatted(buffer);
    EXPECT_TRUE(sci_formatted.find("e") != std::string::npos);
}

/**
 * 测试十进制数值精度损失
 */
TEST_F(DecimalComprehensiveTest, TestPrecisionLoss) {
    // 测试浮点数精度损失
    double a = 0.1;
    double b = 0.2;
    double sum = a + b;
    
    // 由于浮点数精度问题，0.1 + 0.2 != 0.3
    EXPECT_FALSE(sum == 0.3);
    EXPECT_NEAR(sum, 0.3, 1e-15);
    
    // 测试大数值精度
    double large = 1e15;
    double small = 1.0;
    double result = large + small - large;
    
    // 可能由于精度问题，结果不等于1.0
    EXPECT_TRUE(result >= 0.0 && result <= 2.0);
}

/**
 * 测试十进制数值范围检查
 */
TEST_F(DecimalComprehensiveTest, TestRangeValidation) {
    // 测试正常范围
    for (double value : test_values_) {
        EXPECT_TRUE(std::isfinite(value));
        EXPECT_FALSE(std::isnan(value));
        EXPECT_FALSE(std::isinf(value));
    }
    
    // 测试溢出检查
    double large_positive = 1e308;
    double large_negative = -1e308;
    
    EXPECT_TRUE(std::isfinite(large_positive));
    EXPECT_TRUE(std::isfinite(large_negative));
    
    // 测试下溢检查
    double small_positive = 1e-308;
    double small_negative = -1e-308;
    
    EXPECT_TRUE(std::isfinite(small_positive));
    EXPECT_TRUE(std::isfinite(small_negative));
}

/**
 * 测试十进制数值性能特征
 */
TEST_F(DecimalComprehensiveTest, TestPerformanceCharacteristics) {
    const int iterations = 100000;
    std::vector<double> values(iterations);
    
    // 生成测试数据
    for (int i = 0; i < iterations; ++i) {
        values[i] = static_cast<double>(i) / 1000.0;
    }
    
    auto start = std::chrono::high_resolution_clock::now();
    
    // 执行大量十进制运算
    double sum = 0.0;
    for (const auto& value : values) {
        sum += value * 1.1 + 0.01;
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
    
    // 验证结果合理性
    EXPECT_GT(sum, 0.0);
    EXPECT_LT(duration.count(), 100000); // 应该在100ms内完成
    
    std::cout << "十进制运算性能: " << iterations << " 次操作耗时 " 
              << duration.count() << " 微秒" << std::endl;
}

/**
 * 测试多线程环境下的十进制数值操作
 */
TEST_F(DecimalComprehensiveTest, TestMultiThreadDecimalOperations) {
    const int num_threads = 4;
    const int operations_per_thread = 10000;
    std::vector<std::thread> threads;
    std::atomic<int> success_count(0);
    
    for (int t = 0; t < num_threads; ++t) {
        threads.emplace_back([&, t]() {
            for (int i = 0; i < operations_per_thread; ++i) {
                double a = static_cast<double>(t * operations_per_thread + i) / 1000.0;
                double b = a * 2.0;
                double c = b / 2.0;
                
                if (std::abs(c - a) < 1e-10) {
                    success_count.fetch_add(1);
                }
            }
        });
    }
    
    // 等待所有线程完成
    for (auto& thread : threads) {
        thread.join();
    }
    
    // 验证结果
    int expected_success = num_threads * operations_per_thread;
    EXPECT_EQ(success_count.load(), expected_success);
    
    std::cout << "多线程测试结果: " << success_count.load() 
              << "/" << expected_success << " 次操作成功" << std::endl;
}

/**
 * 测试十进制数值的数学函数
 */
TEST_F(DecimalComprehensiveTest, TestMathematicalFunctions) {
    double value = 123.456;
    
    // 测试基本数学函数
    EXPECT_NEAR(std::sqrt(value), 11.1111, 1e-3);
    EXPECT_NEAR(std::pow(value, 2.0), 15241.383936, 1e-6);
    EXPECT_NEAR(std::log(value), 4.8158, 1e-4);
    EXPECT_NEAR(std::exp(1.0), 2.71828, 1e-5);
    
    // 测试三角函数
    double angle = 1.0; // 弧度
    EXPECT_NEAR(std::sin(angle), 0.8414, 1e-4);
    EXPECT_NEAR(std::cos(angle), 0.5403, 1e-4);
    EXPECT_NEAR(std::tan(angle), 1.5574, 1e-4);
    
    // 测试取整函数
    double test_val = 123.789;
    EXPECT_EQ(std::floor(test_val), 123.0);
    EXPECT_EQ(std::ceil(test_val), 124.0);
    EXPECT_EQ(std::round(test_val), 124.0);
}

/**
 * 测试十进制数值的完整工作流程
 */
TEST_F(DecimalComprehensiveTest, TestCompleteWorkflow) {
    // 模拟完整的十进制数值处理流程
    
    // 1. 输入数据
    std::vector<double> input_data = {100.0, 200.5, 300.75, 400.25};
    
    // 2. 数据验证
    for (const auto& value : input_data) {
        EXPECT_TRUE(std::isfinite(value));
        EXPECT_GT(value, 0.0);
    }
    
    // 3. 数据处理
    std::vector<double> processed_data;
    for (const auto& value : input_data) {
        double processed = value * 1.1 + 10.0; // 加价10%再加10
        processed_data.push_back(processed);
    }
    
    // 4. 结果验证
    EXPECT_EQ(processed_data.size(), input_data.size());
    for (size_t i = 0; i < input_data.size(); ++i) {
        double expected = input_data[i] * 1.1 + 10.0;
        EXPECT_NEAR(processed_data[i], expected, 1e-10);
    }
    
    // 5. 统计计算
    double sum = 0.0;
    for (const auto& value : processed_data) {
        sum += value;
    }
    double average = sum / processed_data.size();
    
    EXPECT_GT(sum, 0.0);
    EXPECT_GT(average, 0.0);
    EXPECT_NEAR(average, sum / 4.0, 1e-10);
    
    std::cout << "工作流程测试完成: 处理了 " << input_data.size() 
              << " 个数值，平均值为 " << average << std::endl;
} 